package com.example;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import com.example.androidhive.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidBuildingMusicPlayerActivity  implements OnCompletionListener {
/*
	private ImageButton btnPlay;
	private ImageButton btnForward;
	private ImageButton btnBackward;
	private ImageButton btnNext;
	private ImageButton btnPrevious;
	private ImageButton btnPlaylist;
	private ImageButton btnRepeat;
	private ImageButton btnShuffle;
	private SeekBar songProgressBar;
	private TextView songTitleLabel;
	private TextView songCurrentDurationLabel;
	private TextView songTotalDurationLabel;*/
	// Media Player
	private  MediaPlayer mp;
	// Handler to update UI timer, progress bar etc,.
/*	private Handler mHandler = new Handler();;
	//private SongsManager songManager;
	//private Utilities utils;
	private int seekForwardTime = 5000; // 5000 milliseconds
	private int seekBackwardTime = 5000; // 5000 milliseconds
	private int currentSongIndex = 0; 
	private boolean isShuffle = false;
	private boolean isRepeat = false;
	private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();*/
	Context vv;
	public AndroidBuildingMusicPlayerActivity(Context aa,int a){
		vv=aa;
		//setContentView(R.layout.player);
		
		// All player buttons
	/*	btnPlay = (ImageButton) findViewById(R.id.btnPlay);
		btnForward = (ImageButton) findViewById(R.id.btnForward);
		btnBackward = (ImageButton) findViewById(R.id.btnBackward);
		btnNext = (ImageButton) findViewById(R.id.btnNext);
		btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
		btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist);
		btnRepeat = (ImageButton) findViewById(R.id.btnRepeat);
		btnShuffle = (ImageButton) findViewById(R.id.btnShuffle);
		songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
		songTitleLabel = (TextView) findViewById(R.id.songTitle);
		songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
		songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);*/
		
		// Mediaplayer
	//	mp = new MediaPlayer();
		//songManager = new SongsManager();
	//	utils = new Utilities();
		
		// Listeners
	//	songProgressBar.setOnSeekBarChangeListener(this); // Important
	//	mp.setOnCompletionListener(this); // Important
		
		// Getting all songs list
	//	songsList = songManager.getPlayList();
		
		// By default play first song
		if (a==1)
			playSong(0);
				
		/**
		 * Play button click event
		 * plays a song and changes button to pause image
		 * pauses a song and changes button to play image
		 * */
	/*	btnPlay.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(mp.isPlaying()){
					if(mp!=null){
						mp.pause();
						btnPlay.setImageResource(R.drawable.btn_play);
					}
				}else{
					if(mp!=null){
						mp.start();
						btnPlay.setImageResource(R.drawable.btn_pause);
					}
				}
				
			}
		});*/
		
		/**
		 * Forward button click event
		 * Forwards song specified seconds
		 * */
		/*btnForward.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				int currentPosition = mp.getCurrentPosition();
				if(currentPosition + seekForwardTime <= mp.getDuration()){
					mp.seekTo(currentPosition + seekForwardTime);
				}else{
					mp.seekTo(mp.getDuration());
				}
			}
		});*/
		
		/**
		 * Backward button click event
		 * Backward song to specified seconds
		 * */
	/*	btnBackward.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				int currentPosition = mp.getCurrentPosition();
				if(currentPosition - seekBackwardTime >= 0){
					mp.seekTo(currentPosition - seekBackwardTime);
				}else{
					mp.seekTo(0);
				}
				
			}
		});*/
		
		/**
		 * Next button click event
		 * Plays next song by taking currentSongIndex + 1
		 * */
	/*	btnNext.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(currentSongIndex < (songsList.size() - 1)){
					playSong(currentSongIndex + 1);
					currentSongIndex = currentSongIndex + 1;
				}else{
					playSong(0);
					currentSongIndex = 0;
				}
				
			}
		});*/
		
		/**
		 * Back button click event
		 * Plays previous song by currentSongIndex - 1
		 * */
	/*	btnPrevious.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(currentSongIndex > 0){
					playSong(currentSongIndex - 1);
					currentSongIndex = currentSongIndex - 1;
				}else{
					playSong(songsList.size() - 1);
					currentSongIndex = songsList.size() - 1;
				}
				
			}
		});*/
		
		/**
		 * Button Click event for Repeat button
		 * Enables repeat flag to true
		 * */
	/*	btnRepeat.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(isRepeat){
					isRepeat = false;
					Toast.makeText(getApplicationContext(), "Repeat is OFF", Toast.LENGTH_SHORT).show();
					btnRepeat.setImageResource(R.drawable.btn_repeat);
				}else{
					isRepeat = true;
					Toast.makeText(getApplicationContext(), "Repeat is ON", Toast.LENGTH_SHORT).show();
					isShuffle = false;
					btnRepeat.setImageResource(R.drawable.btn_repeat_focused);
					btnShuffle.setImageResource(R.drawable.btn_shuffle);
				}	
			}
		});*/
		
		/**
		 * Button Click event for Shuffle button
		 * Enables shuffle flag to true
		 * */
	/*	btnShuffle.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(isShuffle){
					isShuffle = false;
					Toast.makeText(getApplicationContext(), "Shuffle is OFF", Toast.LENGTH_SHORT).show();
					btnShuffle.setImageResource(R.drawable.btn_shuffle);
				}else{
					isShuffle= true;
					Toast.makeText(getApplicationContext(), "Shuffle is ON", Toast.LENGTH_SHORT).show();
					isRepeat = false;
					btnShuffle.setImageResource(R.drawable.btn_shuffle_focused);
					btnRepeat.setImageResource(R.drawable.btn_repeat);
				}	
			}
		});*/
		
		/**
		 * Button Click event for Play list click event
		 * Launches list activity which displays list of songs
		 * */
	/*	btnPlaylist.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(getApplicationContext(), PlayListActivity.class);
				startActivityForResult(i, 100);			
			}
		});*/
		
	}
	
	/**
	 * Receiving song index from playlist view
	 * and play the song
	 * */
	
	/**
	 * Function to play a song
	 * @param songIndex - index of song
	 * */
	public void  playSong(int songIndex){
		// Play song
		try {
        //	mp.reset();
		//	mp.setDataSource(songsList.get(songIndex).get("songPath"));
			mp = new MediaPlayer();
			 Uri otherPath = Uri.parse("android.resource://com.example.androidhive/raw/asoundimpress.mp3");
        	//System.IO.Directory.GetFiles (uri.AbsolutePath);
		//	mp.setDataSource(songsList.get(songIndex).get("songPath"));
			//InputStream iStream = getApplicationContext().getResources().openRawResource(R.raw.map);
        //	String uri2 = otherPath.toString();
        //	Uri.parse("android.resource://com.example.androidhive/raw/asoundimpress");
			//R.raw.asoundimpress
		//	 mp = MusicPlayer.getInstance();
		 //       AssetFileDescriptor afd = this.getResources().openRawResourceFd(R.raw.das);
		  //      mp.setDataSource(afd.getFileDescriptor());
		      //  mp= MediaPlayer.create( null, R.raw.das);
        //	mp.setDataSource("com.example.R.raw.asoundimpress");
			//mp.setDataSource("file://sdcard/my_sounds/mezzo3.mp3"
             //);
			 //vv.getPackageName()
			  //String fileName = "android.resource://" + vv.getPackageName() + "/" + R.raw.asoundimpress;
			//  mp.setDataSource("android.resource://" + vv.getPackageName() + "/" + R.raw.das);
	//		mp.setDataSource("file:///data/data/com.example/raw/asoundimpress.mp3");
		//	mp.setDataSource(
		//	"android.resource://com.example/raw/asoundimpress.mp3");
			  mp.setDataSource("http://opiekuj1.javaprovider.net/php/asoundimpress.mp3");
			mp.prepare();
			mp.start();
			// Displaying Song title
		//	String songTitle = songsList.get(songIndex).get("songTitle");
        //	songTitleLabel.setText(songTitle);
			
        	// Changing Button Image to pause image
		//	btnPlay.setImageResource(R.drawable.btn_pause);
			
			// set Progress bar values
		//	songProgressBar.setProgress(0);
		//	songProgressBar.setMax(100);
			
			// Updating progress bar
			//updateProgressBar();			
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Update timer on seekbar
	 * */
/*	public void updateProgressBar() {
        mHandler.postDelayed(mUpdateTimeTask, 100);        
    }	*/
	
	/**
	 * Background Runnable thread
	 * */
/*	private Runnable mUpdateTimeTask = new Runnable() {
		   public void run() {
			   long totalDuration = mp.getDuration();
			   long currentDuration = mp.getCurrentPosition();
			  
			   songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
			   songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));
			   
			   int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
			   songProgressBar.setProgress(progress);
			   
		       mHandler.postDelayed(this, 100);
		   }
		};*/
		
	/**
	 * 
	 * */

	/**
	 * When user starts moving the progress handler
	 * */
	
	/**
	 * When user stops moving the progress hanlder
	 * */

	/**
	 * On Song Playing completed
	 * if repeat is ON play same song again
	 * if shuffle is ON play random song
	 * */
	@Override
	public void onCompletion(MediaPlayer arg0) {
		/*
		// check for repeat is ON or OFF
		if(isRepeat){
			// repeat is on play same song again
			playSong(currentSongIndex);
		} else if(isShuffle){
			// shuffle is on - play a random song
			Random rand = new Random();
			currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
			playSong(currentSongIndex);
		} else{
			// no repeat or shuffle ON - play next song
			if(currentSongIndex < (songsList.size() - 1)){
				playSong(currentSongIndex + 1);
				currentSongIndex = currentSongIndex + 1;
			}else{
				// play first song
				playSong(0);
				currentSongIndex = 0;
			}
		}*/
	}
	
	 public void onDestroy(){
	    mp.release();
	 }
	
}